package Boundry;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Boundry.GenericTools.IPanelOperation;
import Controllers.GlobalData;
import GUI.GUIMeanger;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.SwingConstants;
import java.awt.SystemColor;
import java.io.IOException;
import java.awt.Font;

public class MainFrame extends JFrame implements ActionListener
{
	
	private JPanel curpannel;
	private JButton btnHelp;
	private JPanel mainPanel;
	private JPanel slavePanel;
	private JLabel lblUser;
	private JButton btnHome;
	private JLabel lblStepTitle;
	
	public MainFrame() 
	{
		setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/ICONS/12_hardware_dock.png")));
		getContentPane().setBackground(SystemColor.activeCaption);
		setBackground(SystemColor.activeCaption);
		this.setSize(GlobalData.FrameWidth,GlobalData.FrameHeight);
	 	
	 	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	 	
	 	int w = this.getSize().width;
	    int h = this.getSize().height;
	    int x = (dim.width-w)/2;
	    int y = (dim.height-h)/2;

	    // Move the window
	    this.setLocation(x, y);
		getContentPane().setLayout(null);
		
		curpannel=null;
		
	
		
	    slavePanel = new JPanel();
	    slavePanel.setBackground(SystemColor.inactiveCaption);
		slavePanel.setBounds(0, 0, GlobalData.FrameWidth, GlobalData.SlaveHeight);
		getContentPane().add(slavePanel);
		
		mainPanel = new JPanel();
		mainPanel.setBackground(SystemColor.activeCaption);
		mainPanel.setBounds(0, GlobalData.SlaveHeight, GlobalData.FrameWidth,GlobalData.PanelHeight);
		getContentPane().add(mainPanel);
		mainPanel.setLayout(new BorderLayout(0, 0));
		slavePanel.setLayout(null);
			
		btnHelp = new JButton("");
		btnHelp.setIcon(new ImageIcon(MainFrame.class.getResource("/GUI/Image/2_action_help.png")));
		btnHelp.setBounds(10, 5, 65, 41);
		slavePanel.add(btnHelp);
		btnHelp.addActionListener(this);
		
		lblUser = new JLabel("User");
		lblUser.setBounds(640, 11, 186, 32);
		lblUser.setHorizontalAlignment(JLabel.RIGHT);
		slavePanel.add(lblUser);
		
		JLabel label = new JLabel("");
		label.setBounds(836, 11, 32, 32);
		label.setIcon(new ImageIcon(MainFrame.class.getResource("/GUI/Image/6_social_person.png")));
		slavePanel.add(label);
		
		btnHome = new JButton("");
		btnHome.setIcon(new ImageIcon(MainFrame.class.getResource("/ICONS/9_av_previous.png")));
		btnHome.setBounds(84, 5, 65, 41);
		slavePanel.add(btnHome);
		
		lblStepTitle = new JLabel("Step Title");
		lblStepTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblStepTitle.setFont(new Font("Tahoma", Font.PLAIN, 24));
		lblStepTitle.setBounds(221, 17, 431, 29);
		slavePanel.add(lblStepTitle);
		btnHome.addActionListener(this);
		setVisible(true);
		setResizable( false );
		//setBackground(SystemColor.activeCaption);
		
		disappearSlavePanel();
		
		
		this.addWindowListener(new WindowAdapter()
		{
		      public void windowClosing(WindowEvent e)
		      {
		          exitProcedure();
		      }
		});
		
		
		
	}
	private void exitProcedure()
	{
		if (GlobalData.client!=null){
			if (GlobalData.user!=null)
			{
				GlobalData.user.logoff();
			}
			try {
				GlobalData.client.closeConnection();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("See YA");
			
			
		}
		else
			System.out.println("See YA Also");
		
		
		
		this.dispose();
	}
	public void switchScreen(JPanel pan){
		System.out.println("switchScreen - MainFrame");
		

		if ( curpannel!=null  )
			this.mainPanel.remove(curpannel);
		
		curpannel = pan;
		System.out.println(curpannel.toString());

		this.mainPanel.add(curpannel,BorderLayout.CENTER);
		this.curpannel.revalidate();
		this.curpannel.repaint();
		setTitle();
		//curpannel.setVisible(true);
	}
	
	
	public void setUserName(String userName)
	{
		lblUser.setText(userName);
	}
	public void showSlavePanel()
	{
		slavePanel.setVisible(true);
	}
	public void disappearSlavePanel()
	{
		slavePanel.setVisible(false);
	}

	
	public void setTitle()
	{
		if (curpannel instanceof IPanelOperation) 
		{
			String info = ((IPanelOperation)curpannel).setTitle();
			lblStepTitle.setText(info);
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource() == btnHelp)
		{	
			String info = ((IPanelOperation)curpannel).apHelpButton();
			//String info = "Bla Bla Bla";
			JOptionPane.showMessageDialog(this,
					info,
				    "help",
				    JOptionPane.INFORMATION_MESSAGE);
		}
		else if (e.getSource() == btnHome)
		{
			GlobalData.guiMangaer.switchScreen(GlobalData.mainScreen);
		}
	}
	

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame muf = new MainFrame();
					muf.setVisible(true);
					muf.setUserName("Shimon Arzuan");
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
